package com.ele.ui;

import java.util.List;
import java.util.Scanner;

import com.ele.pojo.Food;
import com.ele.pojo.Shop;
import com.ele.service.FoodService;

/**
 * 	菜品管理界面
 * @author candy
 *
 */
public class FoodManager {
	private static Scanner sc = new Scanner(System.in);
	/**
	 * 	添加菜品功能界面
	 * @param shop
	 */
	public void addFood(Shop shop) {
		Food food = new Food();
		
		System.out.println("请输入菜品名字：");
		String foodName = sc.next();
		food.setFoodName(foodName);
		System.out.println("请输入菜品价格：");
		String foodPrice = sc.next();
		food.setFoodPrice(Integer.parseInt(foodPrice));
		System.out.println("请输入菜品描述：");
		String foodDesc = sc.next();
		food.setFoodDesc(foodDesc);
		System.out.println("请输入菜品状态：");
		Integer foodStatus = sc.nextInt();
		food.setFoodStatus(foodStatus);
		
		food.setShop_id(shop.getId());
		
		FoodService foodService = new FoodService();
		Integer flag = foodService.addFood(food);
		if (flag != 0) {
			System.out.println("添加成功");
		}else {
			System.out.println("添加失败");
		}
	}
	/**
	 * 	更新菜品信息界面
	 * @param shop
	 */
	public void updateFood(Shop shop) {
		FoodService foodService = new FoodService();
		System.out.println("请选择想要更新的菜品编号：");
		Integer id = sc.nextInt();
		Food food = foodService.selectFood(shop,id);
		if (food == null) {
			System.out.println("查无此号,请核对");
			updateFood(shop);
		}
		System.out.println(food);
		System.out.println("------------------------------------");
		System.out.println("1.菜名\t2.价格\t3.描述\t4.价格\t5.菜品状态\t6.退出");
		String input = sc.next();
		if (input.equals("1")) {
			System.out.println("请输入更改之后的菜名：");
			food.setFoodName(sc.next());
		}else if (input.equals("2")) {
			System.out.println("请输入更改之后的价格：");
			food.setFoodPrice(Integer.parseInt(sc.next()));
		}else if (input.equals("3")) {
			System.out.println("请输入更改之后的描述：");
			food.setFoodDesc(sc.next());
		}else if (input.equals("4")) {
			System.out.println("请输入更改之后的状态：1.正常 2.售罄 3.下架");
			String flag = sc.next();
			if (flag.equals("1")) {
				food.setFoodStatus(1);
			}else if (flag.equals("2")) {
				food.setFoodStatus(0);
			}else if (flag.equals("3")) {
				food.setFoodStatus(2);
			}else {
				System.out.println("输入不正确");
			}	
		}else {
			System.out.println("输入错误");
		}
		System.out.println("是否继续修改？y/n");
		if (sc.nextLine().equalsIgnoreCase("y")) {
			updateFood(shop);
		}else {
			int x = foodService.updateFood(food);
			if (x > 0) {
				System.out.println("修改成功");
			}
		}
		
	}
	/**
	 * 	查询所有菜品信息界面
	 * @param shop
	 */
	public void selectAllFood(Shop shop) {
		FoodService foodService = new FoodService();
		List<Food> foodList = foodService.selectFood(shop);
		for (Food food: foodList ) {
			System.out.println("----------------");
			System.out.println(food);
		}
		System.out.println("================");
	}
	/**
	 * 	搜索菜品信息界面
	 * @param shop
	 */
	public void selectFood(Shop shop) {
		System.out.println("搜索菜名：");
		String name = sc.next();
		FoodService foodService = new FoodService();
		List<Food> foodList = foodService.selectFood(shop,name);
		for (Food food: foodList ) {
			System.out.println("----------------");
			System.out.println(food);
		}
		System.out.println("================");
	}
}
